package com.xzx.flink.streamapi.processfunction;

import com.xzx.flink.bean.ClickEvent;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.source.SourceFunction;
import org.apache.flink.util.Collector;

/**
 * @version 1.0
 * @auther xinzhixuan
 * @date 2022/5/4 13:02
 */
public class KeyedProcessFunction_02_EventTime {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        env.addSource(new CustomSource())
                .assignTimestampsAndWatermarks(WatermarkStrategy.<ClickEvent>forMonotonousTimestamps()
                        .withTimestampAssigner(new SerializableTimestampAssigner<ClickEvent>() {
                            @Override
                            public long extractTimestamp(ClickEvent element, long recordTimestamp) {
                                return element.getTimestamp();
                            }
                        }))
                .keyBy(d -> true)
                .process(new KeyedProcessFunction<Boolean, ClickEvent, String>() {
                    @Override
                    public void processElement(ClickEvent value, KeyedProcessFunction<Boolean, ClickEvent, String>.Context ctx, Collector<String> out) throws Exception {

                        out.collect("数据到达，数据事件时间：" + ctx.timestamp());
                        out.collect("数据到达，水位线为：" + ctx.timerService().currentWatermark() + "\n---------分割线-----");
                        // 注册一个10秒后的定时器
                        ctx.timerService().registerEventTimeTimer(ctx.timestamp() + 10 * 1000L);
                    }

                    @Override
                    public void onTimer(long timestamp, KeyedProcessFunction<Boolean, ClickEvent, String>.OnTimerContext ctx, Collector<String> out) throws Exception {
                        out.collect("定时器触发，触发时间：" + timestamp);
                    }
                })
                .print();

        env.execute(KeyedProcessFunction_02_EventTime.class.getSimpleName());
    }

    public static class CustomSource implements SourceFunction<ClickEvent> {

        @Override
        public void run(SourceContext<ClickEvent> ctx) throws Exception {
            // 直接发出测试数据
            ctx.collect(new ClickEvent("Mary", "./home", 1000L));
            // 为了更加明显，中间停顿 5 秒钟
            Thread.sleep(5000L);

            // 发出 10 秒后的数据
            ctx.collect(new ClickEvent("Mary", "./home", 11000L));
            Thread.sleep(5000L);

            // 发出 10 秒+1ms 后的数据
            ctx.collect(new ClickEvent("Alice", "./cart", 11001L));
            Thread.sleep(5000L);
        }

        @Override
        public void cancel() {

        }
    }
}
